<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Default_Form_ValidateContact{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $warranty = false){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validateKey = new Zend_Validate();
        $validateMessage = new Zend_Validate();
        $validateEmail = new Zend_Validate();
        $validatePhoneNumber = new Zend_Validate();
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true)
                        ->addValidator(new Zend_Validate_Alnum(true), true);
        
        $option = array('table' => 'product_keys', 'field' => 'key');
        
        $validateKey->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 50)), true)
                        ->addValidator(new Zend_Validate_Db_RecordExists($option), true);
        
        $validateMessage->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 255)), true);
        
        $validateEmail->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 255)), true)
                        ->addValidator(new Zend_Validate_EmailAddress(), true);
        
        $validatePhoneNumber->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_StringLength(array('max' => 11)), true)
                        ->addValidator(new Zend_Validate_Digits(true), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label id="name" class="required"><em>' . $translate->_('name') . current($validateName->getMessages()) . '</em></label>';
            $this->_isError = true;
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
        }
        
        /*
        * Check Key
        */
        
        if($warranty){
            if(!$validateKey->isValid($params['key'])){
                $this->_messageError['key'] = '<label id="name" class="required"><em>' . $translate->_('key error') . '</em></label>';
                $this->_isError = true;
            }else{
                $this->_messageError['key'] = '';
                $this->_arrData['key'] = $params['key'];
            }
        }
        
        /*
        * Check Message
        */
        $field = 'message';
        
        if($warranty){
            $field = 'reason';
        }
        
        if(!$validateMessage->isValid($params['message'])){
            $this->_messageError['message'] = '<label id="message" class="required"><em>' . $translate->_($field) . current($validateMessage->getMessages()) . '</em></label>';
            $this->_isError = true;
        }else{
            $this->_messageError['message'] = '';
            $this->_arrData['message'] = $params['message'];
        }
        
        /*
        * Check Email
        */
        
        if(!$validateEmail->isValid($params['email'])){
            $this->_messageError['email'] = '<label id="email" class="required"><em>' . $translate->_('email') . current($validateEmail->getMessages()) . '</em></label>';
            $this->_isError = true;
        }else{
            $this->_messageError['email'] = '';
            $this->_arrData['email'] = $params['email'];
        }
        
        /*
        * Check Phone Number
        */
        
        if(!$validatePhoneNumber->isValid($params['phone_number'])){
            $this->_messageError['phone_number'] = '<label id="phone_number" class="required"><em>' . $translate->_('phone number') . current($validatePhoneNumber->getMessages()) . '</em></label>';
            $this->_isError = true;
        }else{
            $this->_messageError['phone_number'] = '';
            $this->_arrData['phone_number'] = $params['phone_number'];
        }
        
        $this->_arrData['type'] = $params['type'];
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}